package com.cloud.bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class BIOService {
    public static void handler(Socket socket){
        try {
            byte[] bytes = new byte[1024];
            InputStream inputStream = socket.getInputStream();
            while (true){
                //读取字节流
                int reader = inputStream.read(bytes);
                if(reader != -1){
                    System.out.println(new String(bytes,0,reader));
                }
                else {
                    break;
                }
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        finally {
            try {
                System.out.println("socket关闭");
                socket.close();
            }
            catch (Exception ex){
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(12345);
        System.out.println("BIO服务器启动----------");
        while (true){
            //获取socket
            final Socket socket = serverSocket.accept();
            System.out.println("有一个新连接--------------");
            handler(socket);
        }
    }

}
